<template>
  <div class="xtx-form">
    <Form
      ref="target"
      autocomplete="off"
      :validationSchema="rules"
      v-slot="{ errors }"
    >
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-user"></i>
          <Field
            name="account"
            v-model="form.account"
            class="input"
            type="text"
            placeholder="请输入用户名"
          />
        </div>
        <div v-if="errors.account" class="error">
          {{ errors.account }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-phone"></i>
          <Field
            name="mobile"
            v-model="form.mobile"
            class="input"
            type="text"
            placeholder="请输入手机号"
          />
        </div>
        <div v-if="errors.mobile" class="error">
          {{ errors.mobile }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-code"></i>
          <Field
            name="code"
            v-model="form.code"
            class="input"
            type="text"
            placeholder="请输入验证码"
          />
          <span class="code" @click="send">{{
            time === 0 ? '发送验证码' : `${time}s后发送`
          }}</span>
        </div>
        <div v-if="errors.code" class="error">
          {{ errors.code }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field
            name="password"
            v-model="form.password"
            class="input"
            type="password"
            placeholder="请输入密码"
          />
        </div>
        <div v-if="errors.password" class="error">
          {{ errors.password }}
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field
            name="rePassword"
            v-model="form.rePassword"
            class="input"
            type="password"
            placeholder="请确认密码"
          />
        </div>
        <div v-if="errors.rePassword" class="error">
          {{ errors.rePassword }}
        </div>
      </div>
      <a href="javascript:;" class="submit" @click="submit">立即提交</a>
    </Form>
  </div>
</template>

<script>
import { Form, Field } from 'vee-validate'
import { reactive, ref } from 'vue'
import { mobile, account, code, password, rePassword } from '@/utils/validate'
import { useCounter } from '@/hooks'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { Message } from '@/components'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'CallbackPatch',
  components: {
    Field,
    Form
  },
  props: {
    unionId: String
  },
  setup(props) {
    const router = useRouter()
    const store = useStore()
    const form = reactive({
      account: '',
      password: '',
      mobile: '',
      code: '',
      rePassword: ''
    })
    const rules = {
      mobile,
      code,
      account,
      password,
      rePassword
    }
    const { time, start } = useCounter()
    const target = ref(null)
    const send = async () => {
      if (time.value > 0) return
      // 校验手机号
      const valid = mobile(form.mobile)
      if (valid !== true) {
        return target.value.setFieldError('mobile', valid)
      }
      // 发送请求
      await userQQPatchCode(form.mobile)
      // 开启倒计时
      start()
    }
    const route = useRoute()
    const submit = async () => {
      // 整体校验
      const valid = await target.value.validate()
      if (!valid) return
      // 发送请求进行完善信息
      const res = await userQQPatchLogin(props.unionId, form)
      // 1. 把res保存到vuex中
      store.commit('user/setProfile', res.result)
      // 2. 合并购物车
      await store.dispatch('cart/mergeLocalCart')
      // 3. 提示消息
      Message({ text: '登录成功' })
      // 4. 跳转到首页
      const redirectUrl = route.query.redirectUrl || '/'
      router.push(redirectUrl)
    }
    return {
      form,
      rules,
      send,
      target,
      time,
      submit
    }
  }
}
</script>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
